package functionalInterface;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author LiuYan
 * @date 2021/11/17 15:08
 * <p>
 * Java8 内置的四大核心函数式接口
 * <p>
 * Consumer<T>:消费型接口
 * void accept(T t);
 * <p>
 * Supplier<T>:供给型接口
 * T get();
 * <p>
 * Function<T,R>:函数型接口
 * R apply(T t);
 * <p>
 * Predicate<T>:断言型接口
 * boolean test(T t);
 */
public class Part02 {

    public static void main(String[] args) {
        happy(100, x -> System.out.println("今天消费" + x));
        System.out.println(getNumList(10, () -> (int) (Math.random() * 100)));
        System.out.println(strHandler("\t\t\t 学习Java", str -> str.trim()));
        System.out.println(filterStr(Arrays.asList("123", "6789","7878787"), str -> str.length() > 3));
    }

    private static void happy(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    private static List<Integer> getNumList(int num, Supplier<Integer> supplier) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

    private static String strHandler(String str, Function<String, String> function) {
        return function.apply(str);
    }

    private static List<String> filterStr(List<String> strList, Predicate<String> predicate) {
        ArrayList<String> list = new ArrayList<>();
        for (String s : strList
        ) {
            if (predicate.test(s)) {
                list.add(s);
            }

        }
        return list;

    }


}
